package com.le.sarrs.dao.impl;

import com.le.sarrs.dao.IDao;
import com.le.sarrs.web.model.RecommendParam;
import com.letv.honeycomb.recommend.assemble.RecommendAssembleService;
import com.letv.honeycomb.recommend.enums.ProductCode;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import rec.recommend.RecommendationRequest;
import serving.GenericServingRequest;

import javax.annotation.Resource;

@Repository
public class HoneycombDao implements IDao {
    private static final Logger log = LoggerFactory.getLogger(HoneycombDao.class);

    @Resource
    private RecommendAssembleService recommendAssembleService; //统一推荐

    @Override
    public JSONObject getRecList(RecommendParam params) {
        GenericServingRequest request = new GenericServingRequest();
        RecommendationRequest rRequest = new RecommendationRequest();
        rRequest.setBucket("");
        rRequest.setArea("");
        request.setRecommendation_request(rRequest);

        String result = recommendAssembleService.recommendStr(request, ProductCode.LEVIEW);
        return JSONObject.fromObject(result);
    }

    @Override
    public String getRec(RecommendParam params) {
        String result = "";
        try {
            GenericServingRequest request = new GenericServingRequest();

            RecommendationRequest rRequest = new RecommendationRequest();
            rRequest.setAction("rec");
            rRequest.setCid(params.getCid());
            rRequest.setUid(params.getUid());
            rRequest.setLc(params.getLc());

            rRequest.setApp_version(params.getVersion());
            //set version ???
            rRequest.setSession_id(params.getSessionid());
            rRequest.setLang(params.getLang());
            rRequest.setVersion_type(params.getVersiontype());
            rRequest.setFrom_(params.getFrom());
            rRequest.setBrand(params.getBrand());
            rRequest.setRegion(params.getLocation().getRegion());

            if ("true".equalsIgnoreCase(params.getEnable_suggest_tag())) {
                rRequest.setEnable_suggest_tag(true);
            } else {
                rRequest.setEnable_suggest_tag(false);
            }

            rRequest.setSuggest_tag(params.getSuggest_tag());

            //todo test使用0602
            rRequest.setArea("rec_0603");

//            if (StringUtils.isBlank(params.getSuggest_tag())) {
//                rRequest.setArea("rec_0603");
//            } else {
//                rRequest.setArea("rec_0604");
//            }

            request.setRecommendation_request(rRequest);
            result = recommendAssembleService.recommendStr(request, ProductCode.LEVIEW);
        } catch (Exception e) {
            log.error("thrift rec error:" + e.getMessage());
        }
        return result;
    }

    @Override
    public String getData(String url) {
        //todo http
        return null;
    }

}
